﻿#include "WidgetBrowserTable.h"
#include <QHeaderView>
#include <QMenu>
#include <QPushButton>
#include <QColorDialog>
#include <QClipboard>
#include "FITK_Kernel/FITKAppFramework/FITKAppFramework.h"
#include "FITK_Kernel/FITKAppFramework/FITKGlobalData.h"
#include "FITK_Component/FITKAcousticsPhysics/FITKAcousticsPhysics.h"

namespace GUI
{
    WidgetBrowserTable::WidgetBrowserTable(QWidget* parent)
    {
        //双击事件
        connect(this, SIGNAL(itemDoubleClicked(QTableWidgetItem*)), this, SLOT(itemDoubleClicked(QTableWidgetItem*)));
        //单击事件
        connect(this, SIGNAL(itemClicked(QTableWidgetItem*)), this, SLOT(itemClicked(QTableWidgetItem*)));

        this->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(contextMenuSlot(const QPoint&)));
        //表头
        QHeaderView* header = this->horizontalHeader();
        if (header == nullptr) return;
        //开启右键
        header->setContextMenuPolicy(Qt::CustomContextMenu);
        connect(header, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(headerContextMenuSlot(const QPoint&)));
    }

    WidgetBrowserTable::~WidgetBrowserTable()
    {
        //最后剩余的handler
        if (_handler != nullptr)
        {
            delete _handler;
            _handler = nullptr;
        }
    }
 

    void WidgetBrowserTable::update(BrowserTableAbstractHandler* h)
    {
        //调用之前的handler
        if (h == nullptr && _handler != nullptr)
            _handler->updateTable();
        
        if (h == nullptr) return;

        //释放已经存在的handler
        if (_handler != nullptr)
        {
            delete _handler;
            _handler = nullptr;
        }
        //记录当前
        _handler = h;
        //刷新表格
        if(_handler)
            _handler->updateTable();
    }


    void WidgetBrowserTable::autoResizeCellWidth()
    { 
        this->resizeColumnsToContents();

        // 设置每列的最小宽度为 120 像素
        for (int i = 0; i < this->columnCount(); ++i)
        {
            int currentWidth = this->columnWidth(i);
            if (currentWidth < 120) {
                this->setColumnWidth(i, 120);
            }
            else if (currentWidth > 350)
            {
                this->setColumnWidth(i, 350);
            }
        }
    }

    void WidgetBrowserTable::setTableItemEditable(bool e, const int &row /*= -1*/, const int &col /*=-1 */)
    {
        if (row == -1&& !e) {
            this->setEditTriggers(QAbstractItemView::NoEditTriggers);
            return;
        }

        const int nRow = this->rowCount();
        const int nCol = this->columnCount();
        for (int i = 0; i<nRow ; ++i)
        {
            //筛选行
            if (row != -1 && i != row) continue;
            for (int j = 0; j < nCol; ++j)
            {
                //列筛选
                if (col != -1 && j != col) continue;

                QTableWidgetItem* tabItem = this->item(i, j);
                if (tabItem == nullptr) continue;
                //可编辑
                if(e)
                    tabItem->setFlags(tabItem->flags() & Qt::ItemIsEditable);
                //不可编辑
                else
                    tabItem->setFlags(tabItem->flags() & ~Qt::ItemIsEditable);

            }
        }

    }

    void WidgetBrowserTable::contextMenuSlot(const QPoint& pos)
    {
        QTableWidgetItem* item = this->itemAt(pos);
        if (item == nullptr ||_handler == nullptr) return;
        //右键
        QMenu menu(this);
        _handler->contextMenu(&menu);
        menu.exec(QCursor::pos());
    }

    void WidgetBrowserTable::headerContextMenuSlot(const QPoint& pos)
    {
        QHeaderView* header = this->horizontalHeader();
        if (header == nullptr) return;
        const int index = header->logicalIndexAt(pos);
        //表头右键
        QMenu menu(this);
        _handler->headerContextMenu(&menu, index);
        menu.popup(pos);
    }

 

    void WidgetBrowserTable::itemDoubleClicked(QTableWidgetItem *item)
    {
        if (item == nullptr || _handler == nullptr) return;
        const int r = item->row();
        const int col = item->column();
        if (r < 0 || col < 0) return;
        _handler->itemDoubleClicked(r, col);
    }

    void WidgetBrowserTable::itemClicked(QTableWidgetItem *item)
    {
        if (item == nullptr || _handler == nullptr) return;
        const int r = item->row();
        const int col = item->column();
        if (r < 0 || col < 0) return;
        _handler->itemClicked(r, col);
    }

    BrowserTableAbstractHandler::BrowserTableAbstractHandler(WidgetBrowserTable* table)
        :_tableWidget(table)
    {
        _physics = Acoustics::FITKAcousticsPhysics::GetAcousticsPhysicsFromFrame();
    }

    BrowserTableAbstractHandler::~BrowserTableAbstractHandler()
    {
    }

    void BrowserTableAbstractHandler::updateTable()
    {
        //虚函数
    }

    void BrowserTableAbstractHandler::updateData()
    {
        //虚函数
    }

    void BrowserTableAbstractHandler::contextMenu(QMenu* popMenu)
    {
        //虚函数
    }

    void BrowserTableAbstractHandler::headerContextMenu(QMenu* popMenu, int coluIndex)
    {
        //虚函数
    }

    void BrowserTableAbstractHandler::itemDoubleClicked(int row, int colum)
    {
        //虚函数
    }

    void BrowserTableAbstractHandler::itemClicked(int row, int colum)
    {
        //虚函数
    }
    void BrowserTableAbstractHandler::copyAsTextSlot()
    {
        if (_tableWidget == nullptr) return;
        QList<QTableWidgetItem*> items = _tableWidget->selectedItems();
        if (items.isEmpty()) return ;
       
        QString text{};

        if (items.size() == 1)
            text = items[0]->text();
        else
        {
            //获取首个元素行号
            int rowNum = items[0]->row();

            for (auto item : items)
            {
                if (!item)
                {
                    text += "\t";
                    continue;
                }
                if (item->row() == rowNum)
                {
                    text += item->text() + "\t";
                }
                else if (item->row() == rowNum + 1)
                {
                    text += "\n"+ item->text() + "\t";
                    ++rowNum;
                }
            }
        }
        QClipboard *clipboard = QApplication::clipboard();
        clipboard->setText(text);
    }

    QList<QTableWidgetItem* > BrowserTableAbstractHandler::getSelectItems()
    {
        if (_tableWidget == nullptr) return QList<QTableWidgetItem*>();
        return _tableWidget->selectedItems();
    }

    QTableWidgetItem* BrowserTableAbstractHandler::getSelectItem()
    {
        if (_tableWidget == nullptr) return nullptr;
        //多选返回最后一个选择
        QList<QTableWidgetItem*> items = _tableWidget->selectedItems();
        if (items.isEmpty()) return nullptr;
        return items.last();
    }

    QAction* BrowserTableAbstractHandler::AddMenuAction(QMenu* menu, const QString& text)
    {
        if (menu == nullptr || text.isEmpty()) return nullptr;
        return menu->addAction(text);
    }

    void BrowserTableAbstractHandler::SetPushButtonColor(QPushButton* button, int r, int g, int b)
    {
        if (button == nullptr) return;
        const QString style = QString("background-color: rgb(%1, %2, %3);").arg(r).arg(g).arg(b);
        button->setStyleSheet(style);
    }

   
}




